﻿using EFCoreRepository.Extensions;
using Microsoft.Data.SqlClient;
using System;
using System.Windows.Forms;
using RescueApp.Data;
using RescueApp.Models;

namespace RescueApp.UI
{
    public partial class frmUpdateRescueApplication : Form
    {
        private RescueContext db;
        private RescueApply rescueApply;

        public frmUpdateRescueApplication()
        {
            InitializeComponent();
        }

        public frmUpdateRescueApplication(RescueContext db, RescueApply rescueApply)
        {
            InitializeComponent();
            this.db = db;
            this.rescueApply = rescueApply;
        }

        private void btn_submit_Click(object sender, EventArgs e)
        {
            // 获取输入值
            string vehicleLicensePlate = txt_vehicleLicensePlate.Text.Trim();
            string carType = txt_carType.Text.Trim();
            string driverLicenseID = txt_driverLicenseID.Text.Trim();
            string phone = txt_phone.Text.Trim();
            string rescueLocation = txt_rescueLocation.Text.Trim();
            string destination = txt_destination.Text.Trim();

            // 验证所有字段是否已填写
            if (string.IsNullOrEmpty(vehicleLicensePlate) ||
                string.IsNullOrEmpty(carType) ||
                string.IsNullOrEmpty(driverLicenseID) ||
                string.IsNullOrEmpty(phone) ||
                string.IsNullOrEmpty(rescueLocation) ||
                string.IsNullOrEmpty(destination))
            {
                MessageBox.Show("请填写完整信息！");
                return;
            }

            rescueApply.VehicleLicensePlate = vehicleLicensePlate;
            rescueApply.CarType = carType;
            rescueApply.RescueLocation = rescueLocation;
            rescueApply.RepairDestination = destination;
            rescueApply.DriverIdentification = driverLicenseID;
            rescueApply.ApplicantPhone = phone;
            rescueApply.ApplicationDate = DateTime.Now;

            string sql = @"UPDATE [dbo].[RescueApply] SET 
                VehicleLicensePlate = @VehicleLicensePlate,
                CarType = @CarType,
                RescueLocation = @RescueLocation,
                RepairDestination = @RepairDestination,
                DriverIdentification = @DriverIdentification,
                ApplicantPhone = @ApplicantPhone,
                ApplicationDate = @ApplicationDate
            WHERE RescueID = @RescueID";

            IDictionary<string, object> dict = new Dictionary<string, object>();
            dict["@VehicleLicensePlate"] = rescueApply.VehicleLicensePlate;
            dict["@CarType"] = rescueApply.CarType;
            dict["@RescueLocation"] = rescueApply.RescueLocation;
            dict["@RepairDestination"] = rescueApply.RepairDestination;
            dict["@RescueID"] = rescueApply.RescueID;

            dict["@DriverIdentification"] = rescueApply.DriverIdentification;
            dict["@ApplicantPhone"] = rescueApply.ApplicantPhone;
            dict["@ApplicationDate"] = DateTime.Now;

            SqlParameter[] parameters = dict.ToSqlParameters();
            int affected = db.ExecuteSql(sql, parameters);

            if (affected > 0)
            {
                MessageBox.Show("救援申请更新成功！");
                this.DialogResult = DialogResult.OK;
                this.Close();
            }
            else
            {
                MessageBox.Show("救援申请更新失败！");
            }
        }

        private void btn_cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void frmUpdateRescueApplication_Load(object sender, EventArgs e)
        {
            txt_vehicleLicensePlate.Text = rescueApply.VehicleLicensePlate;
            txt_carType.Text = rescueApply.CarType;
            txt_rescueLocation.Text = rescueApply.RescueLocation;
            txt_destination.Text = rescueApply.RepairDestination;
            txt_driverLicenseID.Text = rescueApply.DriverIdentification;
            txt_phone.Text = rescueApply.ApplicantPhone;
        }

    }
}